package com.tbit.uqbike.service.business.impl;

import com.tbit.uqbike.manager.exception.BaseException;
import com.tbit.uqbike.object.pojo.AccountConfig;
import com.tbit.uqbike.object.pojo.AppointMent;
import com.tbit.uqbike.object.pojo.User;
import com.tbit.uqbike.service.business.AppointMentService;
import com.tbit.uqbike.webmanager.dao.core.AccountConfigDao;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.AppointMentDao;
import com.tbit.uqbike.webmanager.dao.core.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author jing
 * @date 2020/9/2117:48
 * @Description:
 */
@Service
public class AppointMentServiceImpl implements AppointMentService {
    @Autowired
    private AppointMentDao appointMentDao;
    @Autowired
    private UserDao userDao;
    @Autowired
    private AccountConfigDao accountConfigDao;
    @Autowired
    private AccountDao accountDao;

    /**
     * @param phone
     * @param areaId
     * @param machineNo
     * @param userCode
     * @param pageNo
     * @param rowCount
     * @return java.util.List<com.tbit.uqbike.object.pojo.AppointMent>
     * @Description: 根据传入的参数进行预约记录查询
     * @createDate 2020/9/22 9:22
     */
    @Override
    public List<AppointMent> queryPage(String phone, Integer areaId, String machineNo, String userCode, Integer pageNo, Integer rowCount) {
        Map<String, Object> params = getUserByPhoneAndAreaId(phone, areaId);
        if (null != phone && null == params.get("userId")) {
            return null;
        }
        AccountConfig accountConfig = accountConfigDao.getById(areaId);
        params.put("machineNo", machineNo);
        params.put("userCode", userCode);
        params.put("start", (pageNo - 1) * rowCount);
        params.put("rowCount", rowCount);
        params.put("appointTime", accountConfig == null ? 0 : accountConfig.getAppointTime());
        List<AppointMent> appointMents = appointMentDao.queryPage(params);
        /**过滤掉有效预约*/
        if (appointMents.size() > 0) {
            appointMents = appointMents.stream().filter(appointMent -> {
                if (appointMent != null) {
                    if (appointMent.getRemainingAppointmentTime() < 0) {
                        appointMentDao.del(appointMent.getUserId());
                    }
                    return appointMent.getRemainingAppointmentTime() > 0;
                }
                return false;
            }).collect(Collectors.toList());
        }
        return appointMents;
    }

    @Override
    public Integer getTotal(String phone, Integer areaId, String machineNo, String userCode) {
        Map<String, Object> params = getUserByPhoneAndAreaId(phone, areaId);
        params.put("machineNo", machineNo);
        params.put("userCode", userCode);
        return appointMentDao.getTotal(params);
    }

    @Override
    public void cancal(Integer userId) {
        if (userId == null) {
            throw new BaseException("userId_not_null");
        }
        appointMentDao.del(userId);
    }

    /**
     * @param phone
     * @param areaId
     * @return java.util.Map<java.lang.String, java.lang.Object>
     * @Description: 根据手机号和区域id获取用户id
     * @createDate 2020/9/22 14:54
     */
    @Override
    public Map<String, Object> getUserByPhoneAndAreaId(String phone, Integer areaId) {
        Map<String, Object> params = new HashMap<>();
        params.put("accountId", areaId);
        if (phone != null) {
            params.put("phone", phone);
            /**根据区域id和手机号码获取用户*/
            User user = userDao.getByAdaccountId(params);
            params.put("userId", user == null ? null : user.getUserId());
        }
        return params;
    }
}
